// Fill out your copyright notice in the Description page of Project Settings.


#include "UserWidget/WidgetComponent/ClickedQuestWidget.h"
#include "Components/Border.h"
#include "Components/TextBlock.h"
#include "Components/SizeBox.h"
#include "Components/HorizontalBox.h"
#include "Components/VerticalBox.h"
#include "UserWidget/WidgetComponent/KeyProgressWidget.h"
#include "UserWidget/WidgetComponent/SingleObjectiveHintWidget.h"
#include "UserWidget/WidgetComponent/SingleRewardWidget.h"
#include "UserWidget/WidgetComponent/SingleHintWidget.h"
#include "Libraries/MissionSystemFL.h"
#include "Component/MissionComponent.h"

void UClickedQuestWidget::NativeConstruct() {
	Super::NativeConstruct();
	//UKeyProgressWidget
	Widget_KeyProgress = Cast<UKeyProgressWidget>(GetWidgetFromName(TEXT("Widget_KeyProgress")));
	if (Widget_KeyProgress->IsValidLowLevel()) {
		Widget_KeyProgress->OnProgressCompleted.AddUFunction(this, "OnTrackUpdated");
	}
}

void UClickedQuestWidget::UpdateClickedQuest(FMissionMaster LMissionInfo){
	MissionInfo = LMissionInfo;
	UpdateVisibility();
	UpdateQuestInfo();
	CreateObjectivesList();
	CreateRewardsList();
	UpdateTrackText();
	Widget_KeyProgress->UpdateProgress(0.f, 1.f);
}

void UClickedQuestWidget::CreateObjectivesList(){
	ObjectiveList->ClearChildren();
	TArray<FObjectives> TempObjectives = MissionInfo.Sequences[UMissionSystemFL::GetMissionComponent()->CurrentSequenceIndex].Objectives;
	for (int32 i = 0; i < TempObjectives.Num(); ++i) {
		USingleObjectiveHintWidget* SingleObjectiveHintWidget = Cast<USingleObjectiveHintWidget>(CreateWidget(this, SingleObjectiveHintWidgetClass));
		SingleObjectiveHintWidget->ConstructWidget(TempObjectives[i].ObjectiveLayout.ObjectiveTitle, TempObjectives[i].ObjectiveLayout.Hints, HintWidgetClass);
		ObjectiveList->AddChildToVerticalBox(SingleObjectiveHintWidget);
	}
}

void UClickedQuestWidget::CreateRewardsList(){
	RewardList->ClearChildren();
	TArray<FRewards> TempObjectives = MissionInfo.OnMissionFinished.Rewards;
	for (int32 i = 0; i < TempObjectives.Num(); ++i) {
		USingleRewardWidget* SingleObjectiveHintWidget = Cast<USingleRewardWidget>(CreateWidget(this, SingleRewardWidgetClass));
		SingleObjectiveHintWidget->ConstructWidget(TempObjectives[i]);
		RewardList->AddChildToHorizontalBox(SingleObjectiveHintWidget);
	}
}

void UClickedQuestWidget::UpdateQuestInfo(){
	MissionTitle->SetText(MissionInfo.MissionMainInfo.Name);
	MissionDescription->SetText(MissionInfo.MissionMainInfo.Description);
	const UEnum* LRegion = StaticEnum<ERegion>();
	int32 Index = LRegion->GetIndexByValue((int64)MissionInfo.MissionMainInfo.Region);
	Region->SetText(LRegion->GetDisplayNameTextByIndex(Index));
	RecommendedLevel->SetText(FText::AsNumber(MissionInfo.MissionMainInfo.SuggestedLevel));
}

void UClickedQuestWidget::UpdateTrackText(){
	if (MissionInfo.Repeatable || MissionInfo.MissionCategory != EQuestCategoryStatus::Completed) {
		FMissionMainInfo Temp = UMissionSystemFL::GetMissionComponent()->CurrentMission.MissionMainInfo;
		FText SelectText;
		if (MissionInfo.MissionMainInfo.Name.EqualTo(Temp.Name) && MissionInfo.MissionMainInfo.Description.EqualTo(Temp.Description)) {
			SelectText = FText::FromString(TEXT("UNTRACK"));
		}
		else {
			SelectText = FText::FromString(TEXT("TRACK"));
		}
		TrackText->SetText(SelectText);
		BottomInfo->SetVisibility(ESlateVisibility::SelfHitTestInvisible);
	}
	else {
		BottomInfo->SetVisibility(ESlateVisibility::Collapsed);
	}
}

void UClickedQuestWidget::UpdateVisibility(){
	ClickedMission->SetVisibility(MissionInfo.MissionMainInfo.Name.EqualTo(FText()) ? ESlateVisibility::Collapsed : ESlateVisibility::SelfHitTestInvisible);
}

void UClickedQuestWidget::OnTrackUpdated(bool Filled){
	OnTrackProgressCompleted.Broadcast(Filled);
	UpdateTrackText();
}